
function fileChange(e) {
  var c = e.files;
  if (c && c.length > 0) {
      c = c[0];
      var d = c.name.toLowerCase();
      if (d.indexOf(".bin") != (d.length - 4)) {
          document.getElementById("file-info")
              .value = "";
          alert("选择的文件格式有误");
          return
      }
      if (c.size >= 2*1024*1024) {
          document.getElementById("file-info")
              .value = "";
            alert("选择的文件过大，请选择小于 2MB 的文件");
          return
      }
      document.getElementById("file-name")
          .value = d;
      console.log(document.getElementById("file-info")
          .files)
  }
}

function postOta() {
  timeoutNum = 0;
  var b = document.getElementById("file-info").files;
  var btn = document.getElementById("start-upgrade");
  if (b && b.length > 0) {
    // disable button
    btn.setAttribute("disabled", "disabled");
    b = b[0];
    Ajax.postFile("/setotadata", b, function(a) {
        a = JSON.parse(a);
        // console.log(a);
        if (a.state == 0) {
          alert("升级成功,重启后生效")
        } else {
          alert("升级失败，请稍后重试")
        }
    })
  } else {
    alert("请选择文件")
  }
}

function sendCmd(cmd) {
  var body = {"request": cmd};
  Ajax.postDeviceRequest(COM_CONSTANT.POST_DEVICE_REQUEST_URL, JSON.stringify(body), function(c) {
      c = JSON.parse(c);
      try {
        if(c.status_code == 0){
          alert("命令发送成功")
        }else {
          alert("错误："+c.message)
        }
      } catch (d) {
          alert("错误："+c.message)
      }
  })
}

function getDeviceInfo() {
  var body = {"request": "get_device_info"};
  Ajax.postDeviceRequest(COM_CONSTANT.POST_DEVICE_REQUEST_URL, JSON.stringify(body), function(c) {
      c = JSON.parse(c);
      try {
        if(c.status_code == 0){
          document.getElementById("version-wrap")
              .innerText = c["version"];
        }
      } catch (d) {
          alert("错误："+c.message)
      }
  })
}

function initHash () {
  console.log('页面第一次加载')

  const leftMenuElement = document.getElementById('idLeftMenuOTA');
  leftMenuElement.classList.add('content');

  selectTitle(document.getElementsByClassName('header-title-one')[0])
  selectContent('otaShow')
  
  getDeviceInfo();
}
